@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">{{ __('Register') }}</div>

                <div class="card-body">
                    <form method="POST" action="{{ route('register') }}" id="register-form">
                        @csrf

                        <div class="row mb-3">
                            <label for="email" class="col-md-4 col-form-label text-md-end">{{ __('Email Address') }}</label>

                            <div class="col-md-6">
                                <div class="input-group">
                                    <input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" required autocomplete="email" autofocus>
                                    <button type="button" class="btn btn-outline-secondary" id="send-verification-code">发送验证码</button>
                                </div>

                                @error('email')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>

                        <div class="row mb-3">
                            <label for="captcha" class="col-md-4 col-form-label text-md-end">{{ __('Captcha') }}</label>

                            <div class="col-md-6">
                                <input id="captcha" type="text" class="form-control @error('captcha') is-invalid @enderror" name="captcha" required placeholder="请输入验证码">

                                @error('captcha')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>

                        <div class="row mb-3">
                            <label for="name" class="col-md-4 col-form-label text-md-end">{{ __('Name') }}</label>

                            <div class="col-md-6">
                                <input id="name" type="text" class="form-control @error('name') is-invalid @enderror" name="name" value="{{ old('name') }}" required autocomplete="name">

                                @error('name')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>

                        <div class="row mb-3">
                            <label for="password" class="col-md-4 col-form-label text-md-end">{{ __('Password') }}</label>

                            <div class="col-md-6">
                                <input id="password" type="password" class="form-control @error('password') is-invalid @enderror" name="password" required autocomplete="new-password">

                                @error('password')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>

                        <div class="row mb-3">
                            <label for="password-confirm" class="col-md-4 col-form-label text-md-end">{{ __('Confirm Password') }}</label>

                            <div class="col-md-6">
                                <input id="password-confirm" type="password" class="form-control" name="password_confirmation" required autocomplete="new-password">
                            </div>
                        </div>

                        <div class="row mb-0">
                            <div class="col-md-6 offset-md-4">
                                <button type="submit" class="btn btn-primary" id="register-button">
                                    {{ __('Register') }}
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

@push('scripts')
<script>
// 发送验证码按钮事件
const sendButton = document.getElementById('send-verification-code');

if (sendButton) {
    sendButton.addEventListener('click', function() {
        const email = document.getElementById('email').value;
        
        // 验证邮箱是否为空
        if (!email) {
            alert('请输入邮箱地址');
            return;
        }

        // 验证邮箱格式
        const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
        if (!emailRegex.test(email)) {
            alert('请输入有效的邮箱地址');
            return;
        }

        const button = this;
        button.disabled = true;
        let countdown = 60;
        button.textContent = `${countdown}秒后重试`;

        const timer = setInterval(() => {
            countdown--;
            button.textContent = `${countdown}秒后重试`;
            if (countdown <= 0) {
                clearInterval(timer);
                button.disabled = false;
                button.textContent = '发送验证码';
            }
        }, 1000);

        fetch('{{ route("register.send-verification-code") }}', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]')?.content || '{{ csrf_token() }}',
                'Accept': 'application/json'
            },
            body: JSON.stringify({ email })
        })
        .then(response => response.json())
        .then(data => {
            if (data.status === 'success') {
                alert('验证码已发送到您的邮箱');
            } else {
                alert(data.message || '发送验证码失败，请稍后重试');
                clearInterval(timer);
                button.disabled = false;
                button.textContent = '发送验证码';
            }
        })
        .catch(error => {
            alert('发送验证码失败，请稍后重试');
            clearInterval(timer);
            button.disabled = false;
            button.textContent = '发送验证码';
        });
    });
}

// 注册表单提交事件
const registerForm = document.getElementById('register-form');
if (registerForm) {
    registerForm.addEventListener('submit', function(event) {
        const email = document.getElementById('email').value;
        const captcha = document.getElementById('captcha').value;
        const name = document.getElementById('name').value;
        const password = document.getElementById('password').value;
        const passwordConfirm = document.getElementById('password-confirm').value;
        
        // 前端验证逻辑
        if (!email || !captcha || !name || !password || !passwordConfirm) {
            event.preventDefault();
            alert('请填写所有必填字段');
            return;
        }
        
        if (password !== passwordConfirm) {
            event.preventDefault();
            alert('两次输入的密码不一致');
            return;
        }
    });
}
</script>
@endpush
@endsection
